import array, heapq
assert array.array('f').itemsize == 4
iters = []


def intsfromfile(f):
  while True:
     a = array.array('f')
     a.fromstring(f.read(4000))
     if not a:
         break
     for x in a:
         yield x

inputFileName1 = "/home/diaosi/outputDirForTermScores/outputTest.binary"
inputFileHandler1 = open(inputFileName1,"rb")

outputFileTestName = "test.txt"
outputFileTestHandler = open(outputFileTestName,"w")

iters.append(intsfromfile(inputFileHandler1))

a = array.array('f')
for x in heapq.merge(*iters):
  a.append(x)
  if len(a) >= 1000:
      # old version
      # a.tofile(outputFileHandler)
      # new version
      a = list(a)
      for element in a:
          outputFileTestHandler.write(str(element) + "\n");
      del a[:]
if a:
  # old version
  # a.tofile(outputFileHandler)
  # new version
  a = list(a)
  for element in a:
      outputFileTestHandler.write(str(element) + "\n");  

inputFileHandler1.close()
outputFileTestHandler.close()
print "done"